package com.shqcjd.yunweiyanoperator.ui.activity.remote;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.HiScene.HiDeviceCenterKit.core.HiDeviceKitEngine;
import com.HiScene.HiDeviceCenterKit.core.entity.Device;
import com.HiScene.HiDeviceCenterKit.core.entity.OperationResult;
import com.HiScene.HiDeviceCenterKit.listener.HiDeviceListener;
import com.HiScene.HiDeviceCenterKit.listener.HiMediaDataListener;
import com.HiScene.HiDeviceCenterKit.listener.ListenerManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.ui.activity.WaitingAnswerActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.staff.ContactsDetailsActivity;
import com.shqcjd.yunweiyanoperator.ui.adapter.DeviceListAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * 设备列表
 */
public class DeviceListActivity extends BaseActivity implements HiDeviceListener {


    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_devi_list)
    RecyclerView rvDeviList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private View notDataView;
    private DeviceListAdapter deviceListAdapter;
    private Context mContext;
    private int remoteId;
    private int friendId;
    private String acceptAppCode;
    private String userName;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_device_list;
    }

    @Override
    public void initView() {
        mContext = this;
        tvTitle.setText("请选择设备");
        HiDeviceKitEngine.Instance().init();

        remoteId = getIntent().getIntExtra("remoteId", -1);
        friendId = getIntent().getIntExtra("friendId", -1);
        acceptAppCode = getIntent().getStringExtra("acceptAppCode");
        userName = getIntent().getStringExtra("acceptUserName");

        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvDeviList.getParent(), false);
        initRefreshLayout();
        deviceListAdapter = new DeviceListAdapter();
        rvDeviList.setLayoutManager(new LinearLayoutManager(this));
        rvDeviList.setAdapter(deviceListAdapter);
        deviceListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Device item = (Device) adapter.getItem(position);
                //开始耳机录像
                Intent intent = new Intent(DeviceListActivity.this, WaitingAnswerActivity.class);
                intent.putExtra("remoteId", remoteId);
                intent.putExtra("friendId", friendId);
                intent.putExtra("acceptAppCode", acceptAppCode);
                intent.putExtra("acceptUserName", userName);
                intent.putExtra("deviceSN", item.getSn());
                startActivity(intent);
            }
        });

        checkPermissions();

    }

    @Override
    public void initData() {
        refresh();
    }

    private void checkPermissions() {
        RxPermissions permissions = new RxPermissions(this);
        permissions.setLogging(true);
        permissions.request(
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {

                        } else {
                            ToastUtils.showShort(mContext, "缺少必要权限");
                            finish();
                        }
                    }
                });
    }

    private void initRefreshLayout() {
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

    }

    private void refresh() {
        deviceListAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        swipeLayout.setRefreshing(true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉是重新刷新还是重新获取呢
                List<Device> list = HiDeviceKitEngine.Instance().getDeviceList();
                Logger.i("集合大小:" + list.size());
                if (list.size() > 0) {
                    deviceListAdapter.setNewData(list);
                } else {
                    deviceListAdapter.setNewData(null);
                    deviceListAdapter.setEmptyView(notDataView);
                }

                swipeLayout.setRefreshing(false);
            }
        }, 1000);
    }



    @Override
    protected void onResume() {
        super.onResume();
        HiDeviceKitEngine.Instance().start();
        ListenerManager.getInstance().addHiDeviceListener(this);
//        ListenerManager.getInstance().addHiMediaDataListener(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        HiDeviceKitEngine.Instance().stop();
        ListenerManager.getInstance().removeHiDeviceListener(this);
//        ListenerManager.getInstance().removeHiMediaDataListener(this);


    }

    @Override
    protected void onStop() {
        super.onStop();
//        if (isFinishing()) {
//            HiDeviceKitEngine.Instance().release();
//        }
    }


    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void handleMessageDeviceListChanged() {

    }

    @Override
    public void handleMessageDeviceChanged(String s) {

    }

    @Override
    public void handleMessageFinished(OperationResult operationResult) {

    }

//    @Override
//    public void handleMessageDeviceListChanged() {
//
//    }
//
//    @Override
//    public void handleMessageDeviceChanged(String s) {
//
//    }
//
//    @Override
//    public void handleMessageFinished(OperationResult operationResult) {
//
//    }
//
//    @Override
//    public void onH264Data(byte[] bytes) {
//
//    }
//
//    @Override
//    public void onYuvData(byte[] bytes, int i, int i1) {
//        Logger.i("视频流:" + bytes);
//    }
//
//    @Override
//    public void onPCMFrame(byte[] bytes) {
//
//    }
//
//    @Override
//    public void onAACFrame(byte[] bytes) {
//
//    }
//
//    @Override
//    public void loadVideoComplete() {
//
//    }
}
